package pogvue.gui;

import pogvue.analysis.AAFrequency;
import pogvue.datamodel.SequenceI;
import pogvue.gui.schemes.ColourSchemeI;

import java.awt.*;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public final class LogLikeRenderer implements RendererI {
    protected final Color   color = Color.black;
        
    public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {
	Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);
	return c;
    }
    
    public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText,Vector pid, int seqnum,AlignViewport av, Hashtable props,int intpid[][]) {
	
	int i      = start;
	int length = seq.getLength();

	Color currentColor = Color.white;

	g.setColor(Color.black);

        int window = 500;
        int max    = 100; 

	int offset = window/2;

        int step = window/10;

        SequenceI seq0     = av.getAlignment().getSequenceAt(0);
        if (step > window) {
          step = window;
        }

        if (window < 2) {
           window = 2;
           step = 1;
        }

        int cpgstart = start;
        int cpgend   = end;

	if (cpgstart <= window/2 ) {
	    cpgstart = window/2;
	}
	if (cpgend+window/2 >= length) {
	    cpgend = length-window/2;
	}

	String str1 = seq0.getSequence();
	String str2 = seq.getSequence();

	Hashtable vals = AAFrequency.findLogLikelihood(seq0,seq,cpgstart,cpgend,window,step,av);

	Enumeration en = vals.keys();

	Vector valset = new Vector();
	
	while (en.hasMoreElements()) {
	    valset.add(en.nextElement());
	}

	Collections.sort(valset);
	
	g.setColor(Color.black);
	
	int j = 0;

	while (j < valset.size()) { 
	    
	    Integer posInt  = (Integer)valset.elementAt(j);
	    double  loglike = (Double) vals.get(posInt);
	    int     pos     = posInt;

	    if (loglike > 0) {
		g.setColor(Color.red);
		g.fillRect(x1 + (int)(width*(pos-start)),
			   y1+height/2 - (int)((loglike)*2*height/max),
			   (int)(step*width+1),
			   (int)((loglike)*2*height/max));
	    } else {
		g.setColor(Color.lightGray);
		loglike = -loglike;
		g.fillRect(x1 + (int)(width*(pos-start)),
                y1+height/2,
			   (int)(step*width+1),
			   (int)((loglike)*2*height/max));
		
		
	    }

	    j++;
	}
	
    }
}
